﻿function fieldNumber (objeto){ 
    var evento_key = window.event.keyCode; 
    switch (evento_key){ 
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: break; 
        default: window.event.keyCode = 0;      
        return false;  
    }     
}

//metodos para desempeño
function fieldDecimalNumber (objeto){ 
    var evento_key = window.event.keyCode; 
    switch (evento_key){ 
        case 46:
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: break; 
        default: window.event.keyCode = 0;      
        return false;  
    }     
}
function fieldDecimalNumberNegative (objeto){ 
    var evento_key = window.event.keyCode; 
    switch (evento_key){ 
        case 45:
        case 46:
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: break; 
        default: window.event.keyCode = 0;      
        return false;  
    }     
}
function fieldAlphanumeric (objeto){ 
    var evento_key = window.event.keyCode; 
    if(evento_key >= 97 && evento_key <=122){
       window.event.keyCode = evento_key - 32;
    }
    switch (evento_key){
        case 42:
        case 45: 
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: 
        case 65:
        case 66:
        case 67:
        case 68:
        case 69:
        case 70:
        case 71:
        case 72:
        case 73:
        case 74:
        case 75:
        case 76:
        case 77:
        case 78:
        case 79:
        case 80:
        case 81:
        case 82:
        case 83:
        case 84:
        case 85:
        case 86:
        case 87:
        case 88:
        case 89:
        case 90:
        case 97:
        case 98:
        case 99:
        case 100:
        case 101:
        case 102:
        case 103:
        case 104:
        case 105:
        case 106:
        case 107:
        case 108:
        case 109:
        case 110:
        case 111:
        case 112:
        case 113:
        case 114:
        case 115:
        case 116:
        case 117:
        case 118:
        case 119:
        case 120:
        case 121:
        case 122:break; 
        default: window.event.keyCode = 0;      
        return false;  
    }     
}

function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}
//fin
function SoloNumerosEnteros(){ 
	var key = window.event.keyCode; 
		if ( (key < 48 || key > 57) && key!=190){ 
			window.event.keyCode = 0;
		}
}
 function valFecha(oTxt)
    {
        var bOk = true;
        if (oTxt.value != "__/__/____")
        {
            bOk = bOk && (valDia(oTxt));
            bOk = bOk && (valMes(oTxt));
            bOk = bOk && (valAno(oTxt));           
            
            
            bOk = bOk && (valSep(oTxt));
            
            if (!bOk)
            {
                alert("Fecha inválida");
                oTxt.value = "";
                oTxt.focus();
            }
        }
    } 
    function valDia(oTxt)
    {
        var bOk = false;
        var nDia = parseInt(oTxt.value.substr(0, 2), 10);
        bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
        return bOk;
    }

    function valMes(oTxt){
        var bOk = false;
        var nMes = parseInt(oTxt.value.substr(3, 2), 10);
        bOk = bOk || ((nMes >= 1) && (nMes <= 12));
        return bOk;
    }

    function valAno(oTxt)
    {
        var bOk = true;
        var nAno = oTxt.value.substr(6);
        bOk = bOk && (nAno.length == 4); 
        if (bOk)
        {
            for (var i = 0; i < nAno.length; i++)
            {
                bOk = bOk && esDigito(nAno.charAt(i));
            }
        }
        return bOk;
    }
    function valSep(oTxt){
        var bOk = false;
        bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
        return bOk;
    }
    function esDigito(sChr)
    {
        var sCod = sChr.charCodeAt(0);
        return ((sCod > 47) && (sCod < 58));
    }
    function finMes(oTxt)
    {
        var nMes = parseInt(oTxt.value.substr(3, 2), 10);
        var nRes = 0;
        switch (nMes)
        {
            case 1: nRes = 31; break;
            case 2: 
                if (parseInt(oTxt.value.substr(6)) % 4 == 0)
                {
                    nRes = 29;
                }
                else
                {
                    nRes = 28; 
                }
                
                break;
            case 3: nRes = 31; break;
            case 4: nRes = 30; break;
            case 5: nRes = 31; break;
            case 6: nRes = 30; break;
            case 7: nRes = 31; break;
            case 8: nRes = 31; break;
            case 9: nRes = 30; break;
            case 10: nRes = 31; break;
            case 11: nRes = 30; break;
            case 12: nRes = 31; break;
        }
        return nRes;
    }
                          